package com.yumeng.ui.controller;

import com.yumeng.common.auth.BaseAuthHelper;
import com.yumeng.common.utils.PathUtils;
import com.yumeng.config.helper.EnvHelper;
import com.yumeng.config.properties.YumengAuthProperties;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wxd
 * @date 2024/10/30 15:00
 */
@Slf4j
@Controller
@RequestMapping("/ym/page")
public class YmTemplatesController {

    private final YumengAuthProperties yumengAuthProperties;

    @Autowired
    public YmTemplatesController(YumengAuthProperties yumengAuthProperties) {
        this.yumengAuthProperties = yumengAuthProperties;
    }


    /**
     * 首页
     * @return
     */
    @GetMapping("/index.vw")
    public String index(){
        return "index";
    }

    /**
     * 登录页
     * @return
     */
    @GetMapping("/login.vw")
    public String login(Model model){
        model.addAttribute("loginAction", PathUtils.removeUrlPrefix(yumengAuthProperties.getBasic().getLoginAction()));
        model.addAttribute("usernameParameter", yumengAuthProperties.getBasic().getUsernameParameter());
        model.addAttribute("passwordParameter", yumengAuthProperties.getBasic().getPasswordParameter());
        model.addAttribute("rememberMeParameter", yumengAuthProperties.getBasic().getRememberMeParameter());
        model.addAttribute("captchaParameter", yumengAuthProperties.getBasic().getCaptchaParameter());
        model.addAttribute("captchaIdParameter", yumengAuthProperties.getBasic().getCaptchaIdParameter());
        return "frame/login";
    }

    /**
     * 登录页
     * @return
     */
    @GetMapping("/smsLogin.vw")
    public String smsLogin(Model model){
        model.addAttribute("smsLoginAction", PathUtils.removeUrlPrefix(yumengAuthProperties.getBasic().getSmsLoginAction()));
        model.addAttribute("smsUsernameParameter", yumengAuthProperties.getBasic().getSmsUsernameParameter());
        model.addAttribute("smsPasswordParameter", yumengAuthProperties.getBasic().getSmsPasswordParameter());
        model.addAttribute("rememberMeParameter", yumengAuthProperties.getBasic().getRememberMeParameter());
        return "frame/smsLogin";
    }

    /**
     * 注销页
     * @return
     */
    @GetMapping("/logout.vw")
    public String logout(Model model, HttpServletRequest request){
        model.addAttribute("remoteUser", BaseAuthHelper.getCurrAuthInfo(false).getAuthKey());//request.getRemoteUser()
        model.addAttribute("logoutAction", PathUtils.removeUrlPrefix(yumengAuthProperties.getBasic().getLogoutAction()));
        model.addAttribute("loginPage", PathUtils.removeUrlPrefix(yumengAuthProperties.getBasic().getLoginUrl()));
        return "frame/logout";
    }

    /**
     * 欢迎页
     * @return
     */
    @GetMapping("/welcome.vw")
    public String welcome(Model model, HttpServletRequest request){
        model.addAttribute("remoteUser", BaseAuthHelper.getCurrAuthInfo().getAuthKey());//request.getRemoteUser()
        model.addAttribute("logoutAction", PathUtils.removeUrlPrefix(yumengAuthProperties.getBasic().getLogoutAction()));
        model.addAttribute("loginPage", PathUtils.removeUrlPrefix(yumengAuthProperties.getBasic().getLoginUrl()));
        return "frame/welcome";
    }

    /**
     * 未认证页
     * @return
     */
    @GetMapping("/unAuth.vw")
    public String unAuth(){
        return "frame/unAuth";
    }

    /**
     * 异常页面
     * @return
     */
    @GetMapping("/error.vw")
    public ModelAndView error(String errMsg, HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        map.put("errMsg", errMsg);
        map.put("logoutAction", PathUtils.removeUrlPrefix(yumengAuthProperties.getBasic().getLogoutAction()));
        map.put("loginPage", PathUtils.removeUrlPrefix(yumengAuthProperties.getBasic().getLoginUrl()));
        //TODO 根据不同场景，完善异常详情描述
        //异常详情
        String errDetails = null;
        HttpSession session = request.getSession();
        if (EnvHelper.isDev()){
            Enumeration<String> requestAttrNames = request.getAttributeNames();
            Enumeration<String> sessionAttrNames = session.getAttributeNames();

            log.info("REQUEST_ATTR_KEYS:{}", requestAttrNames);
            log.info("SESSION_ATTR_KEYS:{}", sessionAttrNames);
        }

        Object errDetailsObj = session.getAttribute("SPRING_SECURITY_LAST_EXCEPTION");
        if (errDetailsObj != null){
            if (errDetailsObj instanceof Exception ex){
                errDetails = ex.getMessage();
            }
        }
        map.put("errDetails", errDetails);
        return new ModelAndView("frame/error", map);
    }

}
